programming4us
           
 
 
Programming

Programming with DirectX : Rendering Geometry - Colors

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
10/19/2010 3:43:55 PM
The first property, aside from vertex positions, that we will discuss is vertex colors. In Direct3D a structure called D3DXCOLOR is used to represent colors. The D3DXCOLOR structure is an RGBA (which represents the Red, Green, Blue, and Alpha components of a color where Alpha is used for transparency) structure with floating-point members. A value of 0 represents the absence of color, while 1 represents full color. Anything in between is a percentage between the two extremes. A value of 1, or 100%, represents 255 when working with 8-bit color modes. The D3DXCOLOR structure is as follows.
typedef struct D3DXCOLOR {
FLOAT r;
FLOAT g;
FLOAT b;
FLOAT a;
} D3DXCOLOR, *LPD3DXCOLOR;

Colors Demo

This demo application builds off of the Primitives demo and adds a color property to the vertices that are rendered. In the Primitives demo the pixel shader used yellow for the rendering. In this demo the color is specified per vertex. The first change in the Colors demo is the addition of a D3DXCOLOR object to the vertex structure. The addition of colors to the vertex structure is shown in Listing 1.

Listing 1. The Global Section from the Colors Demo
// Direct3D 10 objects.
ID3D10Device *g_d3dDevice = NULL;
IDXGISwapChain *g_swapChain = NULL;
ID3D10RenderTargetView *g_renderTargetView = NULL;

// Effect objects.
ID3D10Effect *g_shader = NULL;
ID3D10EffectTechnique *g_technique = NULL;

// Display object to store scene geometry.
ID3D10InputLayout *g_layout = NULL;
ID3D10Buffer *g_vertexBuffer = NULL;

// Structure used to represent a single vertex.
struct DX10_Vertex
{
D3DXVECTOR3 pos;
D3DXCOLOR col;
};

Since the vertex structure requires a color now, the data that makes up the object must be updated to reflect this. This can be seen in the initialize function for the Colors demo, where the input layout is given a different property and the list of vertices specifies a color for each vertex point. The initialization code that is specific to the Colors demo is shown in Listing 2. Note that the alignment offset (third to last property for each input element) is 12 for the color element. This is because the element that comes before it takes up 12 bytes, so to get to the color data, Direct3D must move past the first 12 bytes of each vertex to obtain the start of the color.

Listing 2. The Initialize Code Specific to the Colors Demo
bool InitializeDemo()
{
// Load the shader.




// Create the geometry. The layout of each vertex is made up
// of just a position so that is all we need.

D3D10_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12,
D3D10_INPUT_PER_VERTEX_DATA, 0 },
};





DX10_Vertex vertices[] =
{
{ D3DXVECTOR3( 0.0f, 0.5f, 0.5f), D3DXCOLOR(1, 0, 0, 1) },
{ D3DXVECTOR3( 0.5f, -0.5f, 0.5f), D3DXCOLOR(0, 1, 0, 1) },
{ D3DXVECTOR3(-0.5f, -0.5f, 0.5f), D3DXCOLOR(0, 0, 1, 1) },
};
// Create the vertex buffer.

D3D10_BUFFER_DESC buffDesc;

buffDesc.Usage = D3D10_USAGE_DEFAULT;
buffDesc.ByteWidth = sizeof(DX10_Vertex) * 3;
buffDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
buffDesc.CPUAccessFlags = 0;
buffDesc.MiscFlags = 0;

D3D10_SUBRESOURCE_DATA resData;
resData.pSysMem = vertices;

hr = g_d3dDevice->CreateBuffer(&buffDesc, &resData,
&g_vertexBuffer);

if(FAILED(hr))
return false;

return true;
}


The rendering code from the Colors demo is the same as the Primitives demo, with the exception that we are rendering three vertices instead of six. The shader for the Colors demo is slightly different than the Primitives demo. We have added a color semantic to the vertex and pixel shader input structures. This occurs because now we are bringing in a vertex color from the application into the vertex shader input, and we are sending the color from the vertex shader into the pixel shader input. The pixel shader simply uses this incoming vertex color instead of specifying a hard-coded value like the Primitives demo did for its output color value. The shader from the Colors demo is shown in Listing 3. Figure 1 shows a screenshot from the Colors demo. To get a better appreciation of the demo in action it is recommended that you execute it.

Listing 3. The Colors Demo’s Shader
{
float4 Pos : POSITION;
float4 Color : COLOR;
};


struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLORO;
};


PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;

output.Pos = input.Pos;
output.Color = input.Color;

return output;
}


float4 PS(PS_INPUT input) : SV_Target
{
return input.Color;
}
technique10 PassThrough
{
pass P0
{
SetVertexShader(CompileShader(vs_4_0, VS()));

SetGeometryShader(NULL);

SetPixelShader(CompileShader(ps_4_0, PS()));
}
}


Figure 1. A screenshot from the Colors demo.


Other -----------------
- ASP.NET Security : The Membership and Role Management API (part 3) - Role
- ASP.NET Security : The Membership and Role Management API (part 2) - Provider
- ASP.NET Security : The Membership and Role Management API (part 1)
- ASP.NET Security : Security-Related Controls (part 2)
- ASP.NET Security : Security-Related Controls (part 1)
- WCF Security Concepts
- Certificate-Based Encryption
- Encryption Using SSL
- Security Privileges and Services
- Client Credentials
- User-Level Security : Service Credentials
- User-Level Security : Custom Authentication
- User-Level Security : Authorization and Impersonation (part 4) - Impersonation
- User-Level Security : Authorization and Impersonation (part 3) - Security Token Authentication
- User-Level Security : Authorization and Impersonation (part 2) - Claims-Based Authorization
- User-Level Security : Authorization and Impersonation (part 1) - Authorization
- Publisher Certificates
- Using LINQ To SQL
- Service Management API (part 2) - Making API Requests
- Service Management API (part 1)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us